package com.swgk.core.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName TimeUtils
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/10/29 15:01
 * @Version 1.0
 */
public class TimeUtils {

    /**
     * 获取两个日期之间的间隔天数
     *
     * @return
     */
    public static int getGapDayCount(long startTime, long endTime) {
        Calendar from = Calendar.getInstance();
        from.setTime(new Date(startTime));

        Calendar to = Calendar.getInstance();
        to.setTime(new Date(endTime));

        if (to.getTimeInMillis() < from.getTimeInMillis()) {
            return 0;
        }

        return (int) ((to.getTime().getTime() - from.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }


    /**
     * 获取两个日期之间的间隔月数
     *
     * @return
     */
    public static int getGapMonthCount(long startTime, long endTime) {
        Calendar from = Calendar.getInstance();
        from.setTime(new Date(startTime));

        Calendar to = Calendar.getInstance();
        to.setTime(new Date(endTime));

        if (to.getTimeInMillis() < from.getTimeInMillis()) {
            return 0;
        }
        int year1 = to.get(Calendar.YEAR);
        int month1 = to.get(Calendar.MONTH);
        int day1 = to.get(Calendar.DAY_OF_MONTH);

        int month2 = from.get(Calendar.MONTH);
        int year2 = from.get(Calendar.YEAR);
        int day2 = from.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值 假设 d1 = 2015-8-16  d2 = 2011-9-30
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        return yearInterval * 12 + monthInterval;
    }

    /**
     * 获取两个日期之间的间隔年数
     *
     * @return
     */
    public static int getGapYearCount(long startTime, long endTime) {
        Calendar from = Calendar.getInstance();
        from.setTime(new Date(startTime));

        Calendar to = Calendar.getInstance();
        to.setTime(new Date(endTime));
        if (to.getTimeInMillis() < from.getTimeInMillis()) {
            return 0;
        }
        int day = (int) ((to.getTime().getTime() - from.getTime().getTime()) / (1000 * 60 * 60 * 24));
        int year = 0;

        while (day > 365) {
            day = day - 365;
            year++;
        }

        return year;
    }

    /**
     * 按条件返回特定格式文本
     *
     * @return
     */
    public static String getGapCuountTimeFormat(long startTime, long endTime) {
        String timeFormat = "";
        int day = getGapDayCount(startTime, endTime);
        int year = getGapYearCount(startTime, endTime);
        if (day == 0) {//今天
            timeFormat = formatCustomTimeString(startTime, "HH:mm");
        } else if (day == 1) {//昨天
            timeFormat = "昨天";
        } else if (day >= 2 && year == 0) {//一天以上一年以下
//            timeFormat = formatCustomTimeString(startTime, "MM-dd HH:mm");
            timeFormat = formatCustomTimeString(startTime, "MM-dd");
        } else if (year >= 1) {//一年或以上
            timeFormat = String.format("%s年前", year);
//            timeFormat = formatCustomTimeString(startTime, "yyyy-MM-dd");
        }
        return timeFormat;
    }

    /**
     * 时间格式转换
     *
     * @param time         需要转换的时间戳
     * @param formatString 格式举例：yyyy-MM-dd  yyyy-MM-dd  HH:mm:ss  yyyy年MM月dd日
     * @return 返回时间格式
     */
    public static String formatCustomTimeString(long time, String formatString) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
        re_StrTime = sdf.format(new Date(time));
        return re_StrTime;
    }

    public static boolean compareTime10s(long startTime, long endTime) {
        return (endTime - startTime) / 1000 >= 10;
    }

    public static String formatDuringC(long mss) {
        if (mss == 0) {
            return "00:00";
        }
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;

        String d = String.format("%02d", days);
        String h = String.format("%02d", hours);
        String m = String.format("%02d", minutes);
        String s = String.format("%02d", seconds);

        String timeForm = d + " 天 " + h + " 小时 " + m + " 分钟 " + s + " 秒 ";

        if (days == 0) {
            timeForm = h + " 小时 " + m + " 分钟 " + s + " 秒 ";
        }
        if (hours == 0) {
            timeForm = m + "分钟" + s + " 秒 ";
        }
        return timeForm;
    }

    public static String formatDuringN(long mss) {
        if (mss == 0) {
            return "00:00";
        }
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;

        String d = String.format("%02d", days);
        String h = String.format("%02d", hours);
        String m = String.format("%02d", minutes);
        String s = String.format("%02d", seconds);

        String timeForm = d + "天" + h + ":" + m + ":" + s;
        if (days == 0) {
            timeForm = h + ":" + m + ":" + s;
        }
        if (hours == 0) {
            timeForm = m + ":" + s;
        }

        return timeForm;
    }


    /**
     * 获取当前时间-时间戳
     *
     * @return
     */
    public static long getNowTime() {
        return System.currentTimeMillis();
    }

    /**
     * 前/后?天
     *
     * @param d
     * @param day
     * @return
     */
    public static Date rollDay(Date d, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 前/后?月
     *
     * @param d
     * @param mon
     * @return
     */
    public static Date rollMon(Date d, int mon) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.MONTH, mon);
        return cal.getTime();
    }

    /**
     * 前/后?年
     *
     * @param d
     * @param year
     * @return
     */
    public static Date rollYear(Date d, int year) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.YEAR, year);
        return cal.getTime();
    }

    /**
     * 将ms转换成 m′s″或 s″
     *
     * @param ms 毫秒
     * @return m′s″或 s″
     */
    public static String toMinuteAndSecond(long ms) {
        if (ms == 0L) return "0″";
        long minute = ms / 60 * 1000;//分
        long second = (ms - (minute * 60 * 1000)) / 1000; //秒
        if (minute <= 0) {
            return second + "″";
        }
        return minute + "′" + second + "″";
    }

    /**
     * 获取某个日期的时间戳
     *
     * @return
     */
    private static long getMillisNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 获取当年起始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @return
     */
    public static long getYearStartTime(long timeStamp) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /*
     *
     * IM时间展示
     * */
    public static String getIMTimeShow(long startTime) {
        String timeFormat = "";
        long endTime = getNowTime();
        long time = endTime - startTime;
        long toDayTime = getMillisNextEarlyMorning();
        long time1 = startTime - toDayTime;
        long time2 = toDayTime - startTime;
        long currentYearTime = getYearStartTime(startTime);
        if (time1 > 0) {
            if (time < (1000 * 60) && time1 >= (1000 * 60)) {
                timeFormat = "刚刚";
            } else if (time < (1000 * 60 * 60) && time1 >= (1000 * 60 * 60)) {
                timeFormat = String.format("%s分钟前", new DecimalFormat("0").format(Math.floor(time / (1000 * 60))));
            } else if (time < (1000 * 60 * 60 * 24)) {
                timeFormat = String.format("今天 %s", formatCustomTimeString(startTime, "HH:mm"));
            }
        } else if (time2 <= (1000 * 60 * 60 * 24)) {
            timeFormat = String.format("昨天 %s", formatCustomTimeString(startTime, "HH:mm"));
        } else if (time2 > (1000 * 60 * 60 * 24) && startTime >= currentYearTime) {
            timeFormat = formatCustomTimeString(startTime, "MM-dd");
        } else {
            timeFormat = formatCustomTimeString(startTime, "yyyy-MM-dd");
        }
        return timeFormat;
    }
}
